namespace SimpleWebServices.Models
{
    using System.Xml.XPath;
    using SimpleWebServices;
    using SimpleWebServices.Security.Cryptography;
    using SimpleWebServices.Web;
    using SimpleWebServices.Web.UI.Html;
    using SimpleWebServices.Web.UI.Html.Elements;
    using Xunit;

    public sealed class ContentFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<Content>()
                .DerivesFrom<Resource>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("content")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            Content actual = (
                "<content>" +
                    "<div id='greeting'>" +
                        "<p>Hello</p>" +
                    "</div>" +
                "</content>").Deserialize<Content>();

            Assert.NotNull(actual);

            Assert.Equal<Name>("greeting", actual.Div.Id);
            Assert.Equal<string>("Hello", ((actual.Div.Elements[0] as Paragraph).Elements[0] as TextString).Value);
        }

        [Fact]
        public void deserializeEmpty()
        {
            Content actual = "<content />".Deserialize<Content>();

            Assert.NotNull(actual);

            Assert.Null(actual.Div);
        }

        [Fact]
        public void serialize()
        {
            Content obj = new Content
            {
                Div = new Division
                {
                    Class = "greeting",
                    Elements = 
                    {
                        new Paragraph
                        {
                            Elements =
                            {
                                new TextString("Hello")
                            }
                        }
                    }
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/content/div[@class='greeting']/p[text()='Hello'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new Content());
        }

        [Fact]
        public void prop_Div()
        {
            Assert.True(new PropertyExpectations(typeof(Content).GetProperty("Div"))
                .XmlElement("div")
                .IsAutoProperty<Division>()
                .Result);
        }

        [Fact]
        public void prop_Entity()
        {
            Assert.True(new PropertyExpectations(typeof(Content).GetProperty("Entity"))
                .XmlIgnore()
                .TypeIs<string>()
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void prop_Entity_get()
        {
            Content obj = new Content
            {
                Div = new Division
                {
                    Class = "greeting"
                }
            };

            string expected = "UiamtuyZnAmW7Y+1Iy4tbw==";
            string actual = MD5Hash.Compute(obj.Entity);

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void prop_Entity_getEmpty()
        {
            string expected = string.Empty;
            string actual = new Content().Entity;

            Assert.Equal<string>(expected, actual);
        }
    }
}